<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>登录</title>
  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
</head>
<body>
<div>
  请输入用户名：<input type="text" id="username" value="15827073855"/>
</div>
<div>
  请输入密码：  <input type="password" id="password" value="12345678"/>
</div>
<div>
  <button id="btn1">登录</button>
</div>

<div id="result">

</div>
</body>
<script type="text/javascript">
  $(document).ready(function (){
    var token;
    $("#btn1").click(function (){
      var username=$("#username").val();
      var password=$("#password").val();

      $.ajax({
        type: 'post',
        //url: 'http://dorm.zhangqx.com/login',
        url: 'http://localhost:8007/login2',
        //contentType: 'application/json; charset=utf-8',
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'json',
        data: {
          username: username,
          password: password
        },
        success: function (data){

          console.log(data)
          //console.log("进入了成功回调函数，上面是data")
          if(data.status !== 200){
            alert('status='+data.status)
            //alert('登录失败' + data.message)
            $("#result").text("用户名或密码错误！")

          }else{
            alert('登录成功');
            $("#result").text("登录成功！")
            token = data.token; //获得token
            //对localStorage进行操作
            console.log("localStorage success")
            localStorage.setItem("token", token)
            localStorage.setItem("uid", "1")
            console.log(localStorage.getItem("token"))
            console.log(localStorage.getItem("uid"))
          }
        },
        error: function (){
          alert('参数错误')
          $("#result").text('参数错误！')
        }
      })
    })
  })


</script>


</html>